package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/5/13 10:07
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> goods = goodsDao.getGoodsList(offSet,rows,codeOrName,goodsTypeId);
        map.put("total",goods.size());
        map.put("rows",goods);
        return map;
    }

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0?1 :page;
        int offSet = (page -1)* rows;
        List<Goods> goodsList = goodsDao.getListGoods(offSet,rows,goodsName,goodsTypeId);
        map.put("total", goodsList.size());
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public void saveGoods(Goods goods) {
        goods.setLastPurchasingPrice(goods.getPurchasingPrice());
        goods.setInventoryQuantity(0);
        goods.setState(0);

        goodsDao.saveGoods(goods);
    }

    @Override
    public ServiceVO getCode() {
        // 获取最大的编码
        String maxCode = goodsDao.maxCode();
        // 转换成Integer类型+1
        Integer maxCodeInt = Integer.parseInt(maxCode)+1;
        // 转换成 String类型
        String maxCodeStr = maxCodeInt.toString();
        for (int i = 4; i > maxCodeInt.toString().length(); i--) {
            maxCodeStr = "0" + maxCodeStr;
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS,maxCodeStr);
    }

    @Override
    public ServiceVO deleteGoods(Integer goodsId) {
        try {
            Goods goods = goodsDao.getGoodsById(goodsId);
            if (goods.getState() == 0){
                goodsDao.deleteGoods(goodsId);
            }else {
                return new ServiceVO(400, "商品状态不可删除！");
            }
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            return new ServiceVO(400, "商品状态不可删除！");
        }
    }

    @Override
    public void updateGoods(Goods goods) {
        goodsDao.updateGoods(goods);
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0?1 :page;
        int offSet = (page -1)* rows;
        List<Goods> goodsList = goodsDao.getNoInventoryQuantity(offSet,rows,nameOrCode);
        map.put("total", goodsList.size());
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0?1 :page;
        int offSet = (page -1)* rows;
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity(offSet,rows,nameOrCode);
        map.put("total", goodsList.size());
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public void deleteStock(Integer goodsId) {

        goodsDao.updateGoodsById(goodsId);
    }

    @Override
    public Map<String, Object> listAlarm() {
        HashMap<String, Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.listAlarm();
        map.put("rows", goodsList);
        return map;
    }
}
